package leetcode.datastructure.str;

public class Program14LongestCommonPrefix {

    public Program14LongestCommonPrefix() {
    }

    public String longestCommonPrefix(String[] strs) {
        int i = 0;
        while (true) {
            if (strs[0].length() <= i) {
                break;
            }
            char[] charArray = strs[0].toCharArray();
            int length = strs.length;
            int j = 1;
            for (; j < strs.length; j++) {
                if (strs[j].length() <= i || charArray[i] != strs[j].charAt(i)) {
                    break;
                }
            }
            if (j == length) {
                i++;
            } else {
                break;
            }
        }
        return i == 0 ? "" : strs[0].substring(0, i);
    }

    public static void main(String[] args) {
        Program14LongestCommonPrefix obj = new Program14LongestCommonPrefix();
        String[] strings = {"flower","flow","flight"};
        System.out.println(obj.longestCommonPrefix(strings));
        String[] strings1 = {"dog","racecar","car"};
        System.out.println(obj.longestCommonPrefix(strings1));
    }
}
